package com.xy.sql.xml.node.impl;

import com.xy.sql.xml.core.XySQLKit;
import com.xy.sql.xml.fragment.XySQLStringFragment;
import com.xy.sql.xml.itf.XySQLRegister;
import com.xy.sql.xml.kit.XySQLDataUtil;

import com.xy.sql.xml.node.XySQLSuperNode;
import com.xy.sql.xml.core.XySQLTaskContext;
import com.xy.sql.xml.core.XySQLExePara;

import java.util.ArrayList;
import java.util.List;

public class XySQLForeachNode extends XySQLSuperNode {

    @Override
    public String getTag() {
        return "foreach";
    }

    @Override
    public XySQLSuperNode clone() {
        return new XySQLForeachNode();
    }

    //collection="formIdList" index="index" item="formIdListValue" open="(" separator="," close=")"
    private static final String FOR_ATT_COLLECTION = "collection";

    private static final String FOR_ATT_ITEM = "item";

    private static final String FOR_ATT_SEPARATOR = "separator";

    private static final String FOR_ATT_OPEN = "open";

    private static final String FOR_ATT_CLOSE = "close";

    public List<XySQLExePara> boundSql(XySQLTaskContext task) throws Exception {
        List<XySQLExePara> resList = new ArrayList<>();
        //属性定义错误,
        String collection = this.getPropertyMap().get(FOR_ATT_COLLECTION);
        if (collection == null || collection.length() < 0) {
            XySQLRegister.getLogger().debug("foreach 没有配置 collection 属性");
            return resList;
        }
        //不包含这个参数
        Object valueObject = task.getParam().get(collection);
        if (valueObject == null) {
            XySQLRegister.getLogger().debug("foreach 属性" + collection + ", 值为空");
            return resList;
        }
        //转换为列表
        List<Object> valueList = XySQLDataUtil.convertList(valueObject);
        if (valueList.size() == 0) {
            XySQLRegister.getLogger().debug("foreach 属性" + collection + ", 转换为集合为空");
            return resList;
        }
        //属性定义错误
        String key = this.getPropertyMap().get(FOR_ATT_ITEM);
        if (key == null || key.length() < 0) {
            XySQLRegister.getLogger().debug("foreach 没有配置 item 属性");
            return resList;
        }
        //开始符号
        String open = this.getPropertyMap().get(FOR_ATT_OPEN);
        if (open != null && open.length() > 0) {
            resList.addAll(new XySQLStringFragment(open).boundSql(task));
        }
        //分割符号
        String separator = this.getPropertyMap().get(FOR_ATT_SEPARATOR);
        if (separator == null || separator.length() == 0) {
            separator = ",";
        }
        //循环添加
        boolean first = true;
        List<XySQLExePara> listTemp;
        for (Object value : valueList) {
            //添加到参数列表
            task.putPara(key, XySQLKit.convertOne(task, key, value));
            //构建子结构
            for (XySQLSuperNode temp : this.getChildList()) {
                listTemp = temp.boundSql(task);
                if (listTemp != null && listTemp.size() > 0) {
                    if (!first) {
                        resList.addAll(new XySQLStringFragment(separator).boundSql(task));
                    }
                    first = false;
                    resList.addAll(listTemp);
                }
            }
        }
        String close = this.getPropertyMap().get(FOR_ATT_CLOSE);
        if (close != null && close.length() > 0) {
            resList.addAll(new XySQLStringFragment(close).boundSql(task));
        }
        return resList;
    }
}
